package BlogMybatis.blog.aspect;



import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class LogAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //定义要切入的切入点
    @Pointcut("execution(* BlogMybatis.blog.controller.*.*(..))")
    public void log(){}

    //在打日志之前
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        logger.info("------doBefore------");
    }

    @After("log()")
    public void doAfter(){
        logger.info("------doAfter------");
    }

    @AfterReturning(returning = "result",pointcut = "log()")
    public void doAfterRuturn(Object result){
        log.info("result:{}",result);
    }

    public class RequestLog{
        private String url;
        private String ip;
        private String ClassMethod;
        private  Object[] args;

        public RequestLog(String url, String ip, String classMethod, Object[] args) {
            this.url = url;
            this.ip = ip;
            ClassMethod = classMethod;
            this.args = args;
        }

        @Override
        public String toString() {
            return "RequestLog{" +
                    "url='" + url + '\'' +
                    ", ip='" + ip + '\'' +
                    ", ClassMethod='" + ClassMethod + '\'' +
                    ", args=" + Arrays.toString(args) +
                    '}';
        }
    }
}